﻿//           Ascended International
//
//             Copyright (c) 2010
//
//            All Rights Reserved
// ------------------------------------------------------------------------------
//    * DO NOT REMOVE THIS HEADER. DO NOT MODIFY THIS HEADER *
/**********************************************************************************
 * You may use this class for non-commerical purposes. If you wish to use this    *
 * software for a commercial pupose please contact Ascended International at:     *
 * mark@ascended.com.au                                                           *
 *                                                                                *
 * When using this free class, no warranty express or implied is offered.         *
 * In no event shall Ascended International, any of it's employees or shareholds  *
 * be held liable for any direct, indirect, special, exemplary, incidental or     *
 * consequential damages however caused.                                          *
 *                                                                                *
 * If you modify this class, please add your name and the date below as a         *
 * contributor, without removing this notice or modifying it in any way, shape    *
 * or form.                                                                       *
 **********************************************************************************/

using System;

namespace Ascended.System
{
    class ConvertString
    {

        /// <summary>
        /// Converts the specified string representation of a number to an equivalent single-precision floating-point number.
        /// </summary>
        /// <param name="number">A string that contains the number to convert.</param>
        /// <returns>A single-precision floating-point number that is equivalent to the number in value, or 0 (zero) if value is null.</returns>
        public static float ToSingle(string number)
        {
            if (number == null || number == "")
            {
                return 0f;
            }

            bool isNegative;
            int characterPosition;

            int value = 0;
            int dec = 0;
            int mult = 1;

            if (number[0] == '-')
            {
                isNegative = true;
                characterPosition = 1;
            }
            else
            {
                isNegative = false;
                characterPosition = 0;
            }

            char c = '0';
            int stringLength = number.Length;

            for (; characterPosition < stringLength; characterPosition = characterPosition + 1)
            {
                c = number[characterPosition];

                if (c >= '0' && c <= '9')
                {
                    value = (value * 10) + (c - '0');
                }
                else if (c == ',')
                {
                    continue;
                }
                else
                {
                    break;
                }
            }

            if (characterPosition < stringLength && c == '.')
            {
                for (characterPosition = characterPosition + 1; characterPosition < stringLength; characterPosition = characterPosition + 1)
                {
                    c = number[characterPosition];

                    if (c >= '0' && c <= '9')
                    {
                        dec = (dec * 10) + (c - '0');
                        mult = mult * 10;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            float full = value + ((float)dec / mult);//(dec / (float)digits);

            return isNegative ? -full : full;
        }

    }
}